package parserjc.jaccie;

/**
 * @author Ralf Meyer,
 * "Erstellung von Compiler Compiler Komponenten f�r die Zielsprache Java"
 * UniBwM - ID26/96
 *
 * The following methods are implemented by Christoph Reich:
 *
 * addScannerOutput
 */
import java.io.*;
import java.util.*;

public final class SICParserInput extends SICData {

    protected Vector input = new Vector();

    public static SICTree endElement = SICTree.newLeaf("#",null);

    public SICParserInput() {
        input.addElement(endElement);
    }

    public SICParserInput(String[] pin) {
        input.addElement(endElement);
        addInput(pin);
    }

    public SICParserInput( File pinFile ) throws IOException {
        input.addElement(endElement);
        addInput(pinFile);
    }

    public SICParserInput( String pinFileName ) throws IOException {
        input.addElement(endElement);
        addInput(new File(pinFileName));
    }

    public void addInput( String token,String text ) {
        input.insertElementAt(SICTree.newLeaf(token,text),input.size()-1);
    }

    public void addInput( String element ) throws NullPointerException {
        if (element==null) throw new NullPointerException("null input specified");
          else {
            int index = element.indexOf(34);
            if (index==-1) addInput(element,null);
              else if (index>0) addInput(element.substring(0,index-1),
                                         element.substring(index+1,element.length()-1));
          }
    }

    /**
     * This method is temporaere method and should be exchanged when the
     * parser input editor is  build.
     * The token has the form <tokenname> <space> <doublequote> < contents> <doublequote>
     * This should be changed in an array form�.
     * @param token the accumulated token
     */
    public void addScannerOutput(String token) {
        int index = token.indexOf("\"");
        if (index != -1)
            addInput(token.substring(0,index-1).trim(),
                     token.substring(index).trim());
        else addInput(token,null);
    }

    public void addInput( String[] pin ) {
        for (int i=0; i<pin.length; i++) addInput(pin[i]);
    }

    public void addInput( File pinFile ) throws IOException {
        byte[] data = new byte[(int) pinFile.length()];

        FileInputStream in = new FileInputStream(pinFile);
        in.read(data); in.close();

        String cr = System.getProperties().getProperty("line.separator");
        String help = "";
        for (int i=0; i<data.length; i++) {
            if (data[i]>0) help = help + (char)data[i];
              else help = help + (char)(data[i]+256);
            if (help.endsWith(cr) | i==data.length-1) {
               if (help.endsWith(cr)) addInput(help.substring(0,help.length()-cr.length()));
                 else addInput(help);
               help = "";
            }
        }
    }

    public String toString() {
        String cr = System.getProperties().getProperty("line.separator");
        String answer = "";
        String text;
        SICTree element;
        for( int i=0; i<input.size()-1; i++) {
            element = (SICTree)input.elementAt(i);
            answer = answer + element.getValue();
            text = (String)element.getValueOfAttribute("string");
            if (text!=null) answer = answer + (char)171 + text + (char)187;
            answer = answer + cr;
        }
        return answer;
    }

    public String[] asStringArray() {
        String help,text;
        String[] answer = new String[input.size()-1];
        SICTree element;
        for( int i=0; i<answer.length; i++) {
            element = (SICTree)input.elementAt(i);
            help = element.getValue();
            text = (String)element.getValueOfAttribute("string");
            if (text!=null) help = help + (char)171 + text + (char)187;
            answer[i] = help;
        }
        return answer;
    }

    public void save( String pinFileName ) throws IOException {
        save(new File(pinFileName));
    }

    public void save( File pinFile ) throws IOException {
        char[] help = toString().toCharArray();
        byte[] data = new byte[help.length];
        for (int i=0; i<help.length; i++) data[i] = (byte)help[i];
        FileOutputStream out = new FileOutputStream(pinFile);
        out.write(data); out.close();
    }

    public Enumeration elements() {
        return input.elements();
    }

    public SICTree elementAt(int i) {
        return (SICTree)this.input.elementAt(i);
    }

    public int size() {
        return input.size();
    }


}